<!-- 邀请成为帮卖 -->
<template>
  <view class="share_layout_box">
    <canvas class="myCanvas test" :style="'width:'+myCanvasWidth+'px;height:'+myCanvasHeight+'px'" canvas-id="pullNewPostersId"></canvas>
  </view>
</template>
<script>
import share from "@/api/share.js"
export default {
  data () {
    return {
      myCanvasWidth: 280,
      myCanvasHeight: 504,
      dpiMun: 2,
    }
  },
  created () {
    this.myCanvasWidth = this.myCanvasWidth*this.dpiMun
    this.myCanvasHeight = this.myCanvasHeight*this.dpiMun
  },
  methods: {
    async sharePosterFrends(data) {
      wx.showLoading({
        title: '生成中...',
        mask: true
      })
      let _this = this
      let _data = data
      let paramMap = {
        page: _data?.page ?? 'pages/index/index',
        ..._data.options,
        // isHyaline: true,
        // lineColor: {'r':253,'g':253,'b':253},
      }
      share.getSunCode(paramMap).then(async (res_) => {
        if (res_.code == 200) {
          // removeSave() // 先删除在添加
          let pam = {
              ..._data,
              QRCode: res_.data,
            }
            // console.log(pam)
            await _this.createdCode(pam)
            let shareData = {}
            wx.canvasToTempFilePath({
              canvasId: 'pullNewPostersId',
              fileType: 'jpg',
              width: _this.myCanvasWidth,
              height: _this.myCanvasHeight,
              destWidth: _this.myCanvasWidth * 2,
              destHeight: _this.myCanvasHeight * 2,
              success: async (res) => {
                shareData = {
                  code: '200',
                  msg: '绘制成功！',
                  url: res.tempFilePath
                }
                console.log(shareData,'绘制成功')
                // _this.$store.commit('POSTER_SHARING_DATA', shareData)
                _this.$emit('posterShareSuccess', shareData)
                wx.hideLoading()
              },
              fail: function (res) {
                console.log('绘制失败')
                wx.hideLoading()
              }
            },_this)
          // writeFile(res_.data).then(async (img)=>{
          // }).catch(e=>{
          //   wx.hideLoading()
          //   console.log(e)
          // })
        }
      })
    },
    // 开始绘图
    createdCode (data) {
      let _this = this
      let fromData = data
      return new Promise(resolve => {
        exec()
        async function exec () {
          const ctx = wx.createCanvasContext('pullNewPostersId', _this) // 绘图上下文
          let halfW = _this.myCanvasWidth/2
          ctx.setFillStyle('#FFF6E1')
          ctx.fillRect(0, 0, _this.myCanvasWidth, _this.myCanvasHeight)
          ctx.save()
          await _this.drawImg('https://qqposs.oss-cn-shenzhen.aliyuncs.com/mpAppImg/other/pullNewPosters.png', ctx, 0, 0, _this.myCanvasWidth, _this.myCanvasHeight)
          ctx.restore()
          ctx.save()
          ctx.beginPath()
          ctx.strokeStyle = '#fff'
          ctx.arc(32*_this.dpiMun,36*_this.dpiMun,16*_this.dpiMun,0,2*Math.PI)
          ctx.fill()
          ctx.clip()
          await _this.drawImg(fromData.userAvatar, ctx, 16*_this.dpiMun, 20*_this.dpiMun, 32*_this.dpiMun, 32*_this.dpiMun)
          ctx.restore()
          let userNicknameStr = fromData.userNickname.length>24?fromData.userNickname.slice(0,23)+'...':fromData.userNickname
          ctx.setFontSize(14*_this.dpiMun)
          ctx.setFillStyle('#E44435')
          ctx.fillText(userNicknameStr, 56*_this.dpiMun, 42*_this.dpiMun, _this.myCanvasWidth-(56*_this.dpiMun))
          ctx.restore()
          ctx.save()
          ctx.beginPath()
          ctx.setFillStyle('#ffffff')
          ctx.arc(140*_this.dpiMun,412*_this.dpiMun,40*_this.dpiMun,0,2*Math.PI)
          ctx.fill()
          ctx.clip()
          await _this.drawImg(fromData.QRCode, ctx, 104*_this.dpiMun, 376*_this.dpiMun, 72*_this.dpiMun, 72*_this.dpiMun)
          ctx.restore()
          ctx.save()
          ctx.setFontSize(12*_this.dpiMun)
          ctx.setFillStyle('#FFFFFF')
          ctx.fillText(fromData.tipsText, (_this.myCanvasWidth-ctx.measureText(fromData.tipsText).width)/2, _this.myCanvasHeight-(30*_this.dpiMun),_this.myCanvasWidth/2)
          ctx.save()
          ctx.restore()
          ctx.draw(false, () => {
            resolve()
          })
        }
      })
    },
    // 绘制远程网络图片
    async drawImg (url, ctx, startX, startY, width, height) {
      let _this = this
      return new Promise(resolve => {
        exec()
        async function exec () {
          wx.getImageInfo({
            src: url,
            success: function (res) {
              const filePath = res.path
              ctx.drawImage(filePath, startX, startY, width, height)
              resolve()
            },
            fail: function (err) {
              console.log(err)
            },
            complete: function (err) {
              console.log(err)
            }
          })
        }
      })
    }
  }
}
</script>
<style scoped lang="scss">
.share_layout_box {
  position: fixed;
  left: -1000px;
  top: -1000px;
  // z-index: 999;
}
</style>
